import { getToken, setToken, removeToken } from '@/utils/auth'
import { login, logout, getInfo, editUserInfo } from '@/api/user'
import Cookies from 'js-cookie'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    user:null,
    // sidebarRouters: []
  }
}

const state = getDefaultState();

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_USER:( state, user )=>{
    state.user = user
  },
}
const actions = {
  
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password }).then(response => {
         console.log('response111111111:',response);
          const { data } = response
          commit('SET_TOKEN', data);
          setToken(data);
          console.log('data:',data);
          resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      // commit('SET_NAME', 'admin');
      // resolve();
      getInfo().then(response => {
        const { data } = response;
        if(data){
          commit('SET_USER', data);
        }else{
          commit('SET_USER', null);
        }
        // localStorage.setItem('uid',data.id);
        // commit('SET_NAME', data.user_login);
        // let user = data.info;
        // if(user){
        //   commit('SET_USER', {...user,user_login:data.user_login});
        // }else{
        //   commit('SET_USER', {
        //     id:data.id,
        //     user_login:data.user_login
        //   });
        // }
        // commit('SET_ROLES',user.role.map(item=>item.role_id));
        // commit('SET_AVATAR', avatar)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 编辑用户信息
  editUserInfo({ commit,dispatch }, userInfo) {
    return new Promise((resolve, reject) => {
      editUserInfo(userInfo).then(response => {
        if(response.code ==1){
          dispatch('getInfo');
        }
        // if(response.data){
        //   commit('SET_USER', response.data);
        // }else{
        //   commit('SET_USER', null);
        // }
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },


  // user logout
  logout({ commit, dispatch }) {
    return new Promise((resolve, reject) => {
      // logout().then(() => {
        removeToken() // must remove  token  first
        commit('RESET_STATE');
        localStorage.removeItem('uid');
        localStorage.clear();
        Cookies.remove('password');
        Cookies.remove('username');
        commit('SET_NAME', '');
        commit('SET_USER', null);
        resolve()
      // }).catch(error => {
      //   reject(error)
      // })
    })
  },

  // remove token
  resetToken({ commit,dispatch }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE');
      commit('SET_NAME', '');
      commit('SET_USER', {});
      localStorage.removeItem('uid');
      localStorage.clear();
      dispatch('tagsView/delAllViews', null, { root: true })
      resolve()
    })
  },
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
